import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/pages/main_logic.dart';
import 'package:flutter_yn_goods/pages/main_view.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:get/get.dart';

class LoginLogic extends GetxController with GetSingleTickerProviderStateMixin {
  var showPassword = false.obs;
  var textNumber = "".obs;
  var accountEditController = TextEditingController();
  var passwordEditController = TextEditingController();
  var accountAgEditController = TextEditingController();
  var passworAGdEditController = TextEditingController();

  var agreementRx = false.obs;
  var agreementAgRx = false.obs;

  // [log] {account: 09123456781, password: 110105}
  var accountLengthLimit = 32;
  var passwordLengthLimit = 32;

  var canLogin = false.obs;
  var canAgLogin = false.obs;

  late TabController tabController;

  void onTextChange() {
    canLogin.value = accountEditController.text.trim().isNotEmpty &&
        passwordEditController.text.trim().isNotEmpty;

    canAgLogin.value = passworAGdEditController.text.trim().isNotEmpty &&
        passworAGdEditController.text.trim().isNotEmpty;
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose

    super.onClose();
    tabController.dispose();
    accountEditController.dispose();
    passwordEditController.dispose();

    accountAgEditController.dispose();
    passworAGdEditController.dispose();
  }

  @override
  void onInit() {
    // TODO: implement onInit 09123456781
    super.onInit();
    tabController = TabController(length: 2, vsync: this);
    if (kDebugMode) {
      // accountEditController.text = '13438992886';
      // passwordEditController.text = '123123';
      accountEditController.text = '0332530013';
      passwordEditController.text = '1235234';

      accountAgEditController.text = '09123456781';
      passworAGdEditController.text = '110105';

      // accountAgEditController.text = '13438992888';
      // passworAGdEditController.text = '123123';
      // 13438992888

      //会员  258147, agent_phone: 0912345671   uid  110014

      // var accountAgEditController = TextEditingController();
      // var passworAGdEditController = TextEditingController();
      // [log] {account: 09123456781, password: 110105}
      onTextChange();
    }
  }

  void changePasswordState() {
    showPassword.value = !showPassword.value;
  }

  void toRegister() async {
    Get.toNamed(AppRoutes.register,
        arguments: {'isAgency': tabController.index == 1});
  }

  void toLogin() async {
    if (tabController.index == 1 && agreementAgRx.value == false) {
      LoadingUtils.showToast(LocaleKeys.please_agree_terms.tr);
      return;
    } else if (tabController.index == 0 && agreementRx.value == false) {
      LoadingUtils.showToast(LocaleKeys.please_agree_terms.tr);
      return;
    }

    var response = await AppUserManager().login(
        tabController.index == 1
            ? accountAgEditController.text.trim()
            : accountEditController.text.trim(),
        tabController.index == 1
            ? passworAGdEditController.text.trim()
            : passwordEditController.text.trim(),
        isAgency: tabController.index == 1);

    if (response.isSuccess) {
      // Get.offAllNamed(AppRoutes.main);

      Get.back();

    } else {
      LoadingUtils.showToast(
          "${LocaleKeys.login_failed.tr}${response.message}");
    }
  }
}
